from aip import AipFace
import base64
import os
from MeiDuo.settings import BASE_DIR

""" 你的 APPID AK SK """
APP_ID = '20653235'
API_KEY = 'nxAyxkGZUfxdBvQ1G6BGSRXg'
SECRET_KEY = '8g9fHsSzO8KZ2bFbGqkzKBBHGVG8M9Xk'

client = AipFace(APP_ID, API_KEY, SECRET_KEY)


def baseImage(path):
    with open(os.path.join(BASE_DIR, path), "rb") as fp:
        image_data = fp.read()
        data = base64.b64encode(image_data)
        image = data.decode()
        return image


def check():
    image = baseImage("cxk.jpg")

    imageType = "BASE64"

    """ 如果有可选参数 """
    options = {}
    options["face_field"] = "age"
    options["max_face_num"] = 2
    options["face_type"] = "LIVE"
    options["liveness_control"] = "LOW"

    """ 带参数调用人脸检测 """
    res = client.detect(image, imageType, options)
    # print(res["result"])
    print("年龄:", res["result"]["face_list"][0]["age"])
    print(res)


# 注册人脸
def addface():
    image = baseImage("photo.jpg")

    imageType = "BASE64"

    groupId = "1"

    userId = "002"

    """ 调用人脸注册 """
    client.addUser(image, imageType, groupId, userId);

    """ 如果有可选参数 """
    options = {}
    options["user_info"] = "user's info"
    options["quality_control"] = "NORMAL"
    options["liveness_control"] = "LOW"
    options["action_type"] = "REPLACE"

    """ 带参数调用人脸注册 """
    client.addUser(image, imageType, groupId, userId, options)


def searchFace():
    image = baseImage("cxk2.jpg")

    imageType = "BASE64"

    groupIdList = "1,"

    """ 带参数调用人脸搜索 """
    res = client.search(image, imageType, groupIdList)
    print(res["result"])


if __name__ == '__main__':
    searchFace()
